from bdtime import tt
# import pyautogui
# import pydirectinput


# allWindows = pyautogui.getAllWindows()
#
#
# ## 打印所有活动窗口
# for win32Window in allWindows:
#     if win32Window.title == '原神':
#         target_window: pyautogui.Window = win32Window
#     print("打印所有活动窗口:", win32Window)
#
#
# target_window._getWindowRect()
# target_window.left = 0
# target_window.top= 0


import keyboard
import mouse
import pyautogui


is_recurrent_talking = False
is_recurrent_walking = False


click_positions = [
    [1333, 733],
    [1333, 800]
]


def change_is_recurrent_walking_flag():
    global is_recurrent_walking
    is_recurrent_walking = not is_recurrent_walking
    if is_recurrent_walking:
        keyboard.press('w')
    else:
        keyboard.release('w')
    print('~~~ change_is_recurrent_walking_flag:', is_recurrent_walking)

def change_is_recurrent_talking_flag():
    global is_recurrent_talking
    is_recurrent_talking = not is_recurrent_talking

    # event = keyboard.read_event()
    # print('event --- ', event)
    # if event.event_type == keyboard.KEY_DOWN and event.name == 'space':
    #     print('space was pressed')

    print('~~~ change_is_recurrent_flag:', is_recurrent_talking)


from threading import Thread


flag__exist_talking_task = False  # 谈话任务
flag__exist_walking_task = False  # 一直按w向前的任务


# def _start_talking_task():
#     global is_recurrent_walking, flag__exist_walking_task
#     print('===start _start_talking_task, is_recurrent_walking:', is_recurrent_walking)
#
#     break_flag = False
#     while True:
#         while is_recurrent_walking:
#             if tt.stop_alt('s'):
#                 break_flag = True
#                 break
#
#
#
#         if break_flag:
#             break
#
#     flag__exist_walking_task = False
#     print('*** end _start_talking_task, is_recurrent_walking:', is_recurrent_walking)



def _start_talking_task():
    global is_recurrent_talking, flag__exist_talking_task
    print('===start start_talking_task, is_recurrent:', is_recurrent_talking)

    break_flag = False
    while True:
        while is_recurrent_talking:
            if tt.stop_alt('s'):
                break_flag = True
                break

            print('--- on start_talking_task')
            # x, y = mouse.get_position()
            # mouse.move(0, 0)
            for x, y in click_positions:
                # x, y = 1240, 537
                mouse.move(x, y)
                # pyautogui.leftClick()
                pyautogui.click(x, y, interval=0.06)
                tt.sleep(0.1)
                # mouse.click(mouse.LEFT)

            mouse.move(1920, 1280)
            tt.sleep(2)

        if break_flag:
            break

    flag__exist_talking_task = False
    print('*** end start_talking_task, is_recurrent:', is_recurrent_talking)


def start_talking_task():
    global flag__exist_talking_task

    if flag__exist_talking_task:
        return

    flag__exist_talking_task = True
    task = Thread(target=_start_talking_task)
    task.start()


# recorded = mouse.record()
#
# i = 0
# while is_recurrent:
#     if tt.stop_alt('s'):
#         print('--- i:', i)
#         break
#
#     mouse.replay(recorded)
#
#     i += 1
#     print('--- i:', i)

# Block forever, like `while True`.
# mouse.wait()

# keyboard.record(until='esc')
# keyboard.play()



start_talking_task()


keyboard.add_hotkey('alt + w', change_is_recurrent_walking_flag, timeout=3)
keyboard.add_hotkey('alt + a', change_is_recurrent_talking_flag, timeout=3)
# keyboard.add_hotkey('alt + d', start_talking_task)

# Blocks until you press esc.
keyboard.wait('esc')


